package com.example.mypotiota_si.Activity;

import android.content.ContentValues;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.mypotiota_si.Adapter.Dp_shop_dates;
import com.example.mypotiota_si.Adapter.ListViewAdapter_dp_jj_cm;
import com.example.mypotiota_si.Datas.Bmob_shopgouwuche;
import com.example.mypotiota_si.Datas.Shop_dates_io;
import com.example.mypotiota_si.R;
import com.example.mypotiota_si.Sqlitedates.Shop_Shopping_cart_cache;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.UpdateListener;

import static android.view.View.GONE;

public class DianPuActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = "DianPuActivity";
    RecyclerView cm_recyclerView;
    ListViewAdapter_dp_jj_cm cm_adapter;
    private ImageView dp_fh;
    ImageView dp_sc_ys;
    private boolean tf_dp_sc=false;
    LinearLayout dp_sc;
    private String dates;
    private String name;
    private String address;
    private String heart;
    private String yueshou;
    private String shop_address;
    private String shop_delivery_time;
    private String shop_qisong;
    private String shop_peisongfei;
    private String shop_gonggao;
    private TextView dp_name,shop_heart_te,shop_name_te,shop_address_te,shop_qisong_te,shop_time_te,shop_gonggao_te;
    ImageView shop_imager_im;
    RecyclerView dp_cp_recycler_view;
    Cursor cursor;//游标
    SQLiteDatabase sqLiteDatabase;//数据库操作类
    Shop_Shopping_cart_cache shop_shopping_cart_cache;//购物车数据库
    private SharedPreferences sharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dp);
        //隐藏标题栏
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }

        //链接云上数据库执行添加购物车
        Bmob_shopgouwuche bmob_shopgouwuche=new Bmob_shopgouwuche();
        BmobQuery<Bmob_shopgouwuche> bmob_shopgouwucheBmobQuery=new BmobQuery<>();
        bmob_shopgouwucheBmobQuery.findObjects(new FindListener<Bmob_shopgouwuche>() {
            @Override
            public void done(List<Bmob_shopgouwuche> list, BmobException e) {
                if(e==null) {
                    //删除数据
                    for (int i = 0; i < list.size(); i++) {
                        String id = list.get(i).getObjectId();
                        bmob_shopgouwuche.setObjectId(id);
                        bmob_shopgouwuche.delete(new UpdateListener() {
                            @Override
                            public void done(BmobException e) {
                                if (e == null) {
                                } else {
                                    Toast.makeText(DianPuActivity.this, "在购物车数据加载上出现问题：" + e + "建议重新进入", Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                    }
                }else{
                    Toast.makeText(DianPuActivity.this, "在购物车数据加载上出现问题：" + e + "建议重新进入", Toast.LENGTH_SHORT).show();
                }
            }
        });




        //找到控件
        dp_fh = findViewById(R.id.dp_fh);
        dp_sc = findViewById(R.id.dp_sc);
        dp_sc_ys=findViewById(R.id.dp_sc_ys);
        dp_name = findViewById(R.id.dp_name);
        shop_imager_im = findViewById(R.id.shop_imager_im);
        shop_name_te = findViewById(R.id.shop_name_te);
        shop_address_te = findViewById(R.id.shop_address_te);
        shop_qisong_te = findViewById(R.id.shop_qisong_te);
        shop_time_te = findViewById(R.id.shop_time_te);
        shop_gonggao_te = findViewById(R.id.shop_gonggao_te);
        shop_heart_te=findViewById(R.id.shop_heart);
        dp_cp_recycler_view=findViewById(R.id.dp_cp_recycler_view);

        //接受上一个Activity发送的数据
        dates = getIntent().getExtras().getString("dates");
        name = getIntent().getExtras().getString("name");
        address = getIntent().getExtras().getString("address");
        heart = getIntent().getExtras().getString("heart");
        yueshou = getIntent().getExtras().getString("yueshou");
        shop_address = getIntent().getExtras().getString("shop_address");
        shop_delivery_time = getIntent().getExtras().getString("shop_Delivery_time");
        shop_qisong = getIntent().getExtras().getString("shop_qisong");
        shop_peisongfei = getIntent().getExtras().getString("shop_peisongfei");
        shop_gonggao = getIntent().getExtras().getString("shop_gonggao");



        //设置数据
        dp_name.setText(name);
        shop_name_te.setText(name);
        shop_address_te.setText(shop_address);
        shop_qisong_te.setText(shop_qisong+"$");
        shop_time_te.setText("预计"+shop_delivery_time+"送达");
        shop_gonggao_te.setText(shop_gonggao);
        shop_heart_te.setText("评分:"+heart);
        dp_name.setText(name);
        Glide.with(this).load(address).placeholder(R.mipmap.emei_looper1).into(shop_imager_im);
        //解析数据：
        ArrayList<Shop_dates_io> list_shop_dates_ios=new ArrayList<>();
        new Thread(new Runnable() {
            @Override
            public void run() {

                try {
                    JSONArray jsonArray=new JSONArray(dates);
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObject=jsonArray.getJSONObject(i);
                        Shop_dates_io shop_dates_io=new Shop_dates_io();
                        String name=jsonObject.getString("name");
                        String yueshou=jsonObject.getString("yueshou");
                        String imager_address=jsonObject.getString("imager_address");
                        String zan=jsonObject.getString("zan");
                        String pic=jsonObject.getString("pic");
                        String beizhu=jsonObject.getString("beizhu");
                        //添加数据 ：
                        shop_dates_io.setName(name);
                        shop_dates_io.setYueshou(yueshou);
                        shop_dates_io.setImager_address(imager_address);
                        shop_dates_io.setZan(zan);
                        shop_dates_io.setPic(pic);
                        shop_dates_io.setBeizhu(beizhu);
                        list_shop_dates_ios.add(shop_dates_io);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        //创建对应适配器：
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(DianPuActivity.this);
        dp_cp_recycler_view.setLayoutManager(linearLayoutManager);
//        cursor=sqLiteDatabase.query(Shop_Shopping_cart_cache.SHOP_SQLITE_DATES_NAME,null,null,null,null,null,null);
        Dp_shop_dates dp_shop_dates=new Dp_shop_dates(sqLiteDatabase,cursor,list_shop_dates_ios,DianPuActivity.this);
        dp_cp_recycler_view.setAdapter(dp_shop_dates);

        //布局 添加数据
//        LinearLayoutManager layoutManager1 = new LinearLayoutManager(this);
//        layoutManager1.setOrientation(RecyclerView.VERTICAL);
//        cm_recyclerView.setLayoutManager(layoutManager1);
//        cm_adapter = new ListViewAdapter_dp_jj_cm();
//        cm_recyclerView.setAdapter(cm_adapter);
//        dp_sc.setOnClickListener(this);



        //点击事件
        init_dj();

        //循环处理事件---循环访问购物车内容是否存在





    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//
//        ContentValues contentValues;
//        contentValues = new ContentValues();
//        contentValues.put(Shop_Shopping_cart_cache.SHOP_SQLITE_DATES_DT_PIC,"dwadwa");
//        contentValues.put(Shop_Shopping_cart_cache.SHOP_SQLITE_DATES_DT_NAME,"dwadwad");
//        contentValues.put(Shop_Shopping_cart_cache.SHOP_SQLITE_DATES_DT_YUESHOU,"dwadwad");
//        contentValues.put(Shop_Shopping_cart_cache.SHOP_SQLITE_DATES_DT_IMAGER_ADDRESS,"dwawda");
//        contentValues.put(Shop_Shopping_cart_cache.SHOP_SQLITE_DATES_DT_NUMBER,"213");
//        contentValues.put(Shop_Shopping_cart_cache.SHOP_SQLITE_DATES_ID,"0");
//
//        sqLiteDatabase.insert(Shop_Shopping_cart_cache.SHOP_SQLITE_DATES_NAME,null,contentValues);



    }

    //点击事件
    private void init_dj() {
        //点击返回
        dp_fh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.dp_sc:
                dp_sc_ys.setVisibility(GONE);
                if (tf_dp_sc==false){
                    tf_dp_sc=true;
                    dp_sc.setBackgroundResource(R.drawable.emei_sc1);
                    Toast.makeText(this, "收藏成功", Toast.LENGTH_SHORT).show();
                }else {
                    tf_dp_sc=false;
                    dp_sc.setBackgroundResource(R.drawable.emei_sc2);
                    Toast.makeText(this, "取消收藏", Toast.LENGTH_SHORT).show();
                }
                break;
        }

    }
}

